//
//  WJPublishTextVC.m
//  WJBSApp
//
//  Created by dllo on 16/7/30.
//  Copyright © 2016年 王景伟. All rights reserved.
//
//  段子

#import "WJPublishTextVC.h"
#import "WJTextView.h"
#import "WJDataManager.h"
#import "SVProgressHUD.h"
#import "WJPublishModel.h"

@interface WJPublishTextVC ()<UITextViewDelegate>

@property (nonatomic,strong) WJBaseView *touView;

@property (nonatomic,retain) WJTextView *textView;

@property (nonatomic,strong) WJBaseButton *rightbutton;

@end

@implementation WJPublishTextVC


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.textView becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createView];
    
    [self createTextView];
}

- (void)createView
{
    self.touView = [[WJBaseView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    [self.view addSubview:self.touView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(100, 24, WIDTH - 200, 40)];
    lable.text = @"发表文字";
    lable.textAlignment = 1 ;
    [self.touView addSubview:lable];
    
    WJBaseButton *button = [WJBaseButton buttonWithTitle:nil frame:CGRectMake(20, 30, 50, 30) target:self action:@selector(puLeftAction)];
    button.backgroundColor = BSClearColor;
    [button setImage:[UIImage imageNamed:@"navigationButtonReturn.png"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"navigationButtonReturnClick.png"] forState:UIControlStateHighlighted];
    [self.touView addSubview:button];
    
    self.rightbutton = [WJBaseButton buttonWithTitle:@"发表" frame:CGRectMake(WIDTH - 70, 30, 50, 30) target:self action:@selector(puRightAction)];
    [ self.rightbutton setTitleColor:BSBlackColor forState:0];
     self.rightbutton.backgroundColor = BSClearColor;
    [self.touView addSubview: self.rightbutton];
    [self.touView layoutIfNeeded];
    
}

- (void)createTextView
{
    self.textView = [[WJTextView alloc]initWithFrame:CGRectMake(15, 64, WIDTH - 30, 400 )];
    [self.view addSubview:self.textView];
    _textView.alwaysBounceVertical = YES;
    _textView.delegate = self;
    _textView.placeholder = @"这里添加文字,请勿发布色情、政治等违反国家法律的内容,违者封号处理";
    self.editing = YES;
    self.textView.userInteractionEnabled = YES;
    
}




- (void)puLeftAction
{
    [self.textView resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}

//  点击发表进入本地
- (void)puRightAction
{
    [self.textView resignFirstResponder];
    
    NSTimeInterval time=[[NSDate date] timeIntervalSince1970];
    // 美国时间
    NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    dateFormatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    // 北京时间
    NSString*currentDateStr = [dateFormatter stringFromDate:detaildate];
    
    WJPublishModel *model = [[WJPublishModel alloc]init];
    model.type = @"duanzi";
    model.text = self.textView.text;
    model.nowTime = currentDateStr;
    [[WJDataManager shareFMDB]insertModel:model];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"发表成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        [self dismissViewControllerAnimated:YES completion:nil];
    
    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:^{}];

}

#pragma mark -- UITextFieldDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //拖拽时 结束编辑 退出键盘
    [self.textView endEditing:YES];
    [UIView animateWithDuration:0.5 animations:^{
        self.textView.frame = CGRectMake(15, 64, WIDTH - 30, HEIGHT - 64);
    }];
}

- (void)textViewDidChange:(UITextView *)textView{
    //判读是否有文字  设置发表按钮的状态
    self.rightbutton.enabled = textView.hasText;
    if (textView.hasText) {
        
        self.textView.frame = CGRectMake(15, 64, WIDTH - 30,400);
        
    }
}










@end
